package mydessertstore.web.servlet;

import mydessertstore.domain.Account;
import mydessertstore.domain.Cart;
import mydessertstore.domain.CartItem;
import mydessertstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Iterator;

public class CartFormServlet extends HttpServlet {
    private static final String CART_FORM = "/WEB-INF/jsp/cart/cart.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        /*HttpSession session = req.getSession();
        Cart cart = (Cart) session.getAttribute("cart");

        if(cart==null){
            resp.getWriter().print("<script language='javascript'>" +
                    "alert('购物车而为空，仍要查看？');" +
                    "window.location.href='/WEB-INF/jsp/cart/cart.jsp';</script>')");

            System.out.println("购物车为空");


        }*/

        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset = UTF-8");

        //日志功能
        Account account;
        HttpSession session = req.getSession();
        account=(Account) session.getAttribute("account");
        if(account!=null) {

            LogService logService = new LogService();

            //大家根据自己servlet完成的功能把"查看购物车"这里的字符串改成对应的功能
            String logInfo = logService.logInfo(" ") + " 查看购物车" ;
            logService.insertLogInfo(account.getUsername(), logInfo);
        }
        else{
            LogService logService = new LogService();
            String logInfo = logService.logInfo(" ") + " 查看购物车" ;
            logService.insertLogInfo("未登录用户", logInfo);
        }
        //日志结束


            req.getRequestDispatcher(CART_FORM).forward(req, resp);

    }
}
